package se.webbzon.boltzmann.game.object.dialogue;

import javax.vecmath.Vector3d;

import se.webbzon.boltzmann.resource.Resources;
import se.webbzon.oschi01.worldobject.appearance.Animation3D;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.XYPlaneAppearance3D;

public class DialogueArrow extends AbstractDialogueItem {
	
	// The direction of the dialogue arrow
	public static enum Direction {RIGHT, UP, LEFT, DOWN};
	
	// The direction of the arrow
	private final Direction direction;
	
	// The amplitude of the arrow oscillation
	private final double amplitude;
	
	// The phase value
	private double t;
	
	// The coordinates of this arrow as defined by the user
	private double x,y;

	/*============================================================
	Constructors
	============================================================*/
	
	public DialogueArrow(Direction direction, double amplitude) {
		super(createAppearance(direction));
		this.direction = direction;
		this.amplitude = amplitude;
		t = 0;
		
		getAppearance().setAnimation(new ArrowAnimation());
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void setPosition(double x, double y, double z) {
		super.setPosition(x, y, z);
		this.x = x;
		this.y = y;
		t = 0;
	}

	@Override public void getPosition(Vector3d position) {
		super.getPosition(position);
		position.setX(x);
		position.setY(y);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Creates the appearance of this dialogue circle. **/
	private static final Appearance3D createAppearance(Direction direction) {
		XYPlaneAppearance3D appearance = 
				new XYPlaneAppearance3D(0,
										SimpleAppearance3D.TRANSPARENCY+
										SimpleAppearance3D.LAYERS, false);
		appearance.setTextureSet(Resources.getTextureSet("misc_dialogue_arrow"));
		appearance.setTextureIndex(direction.ordinal());
		appearance.setLayer(0, 0, 1);

		return appearance;
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class ArrowAnimation implements Animation3D {

		@Override public void onStepEnd(Appearance3D appearance) {
			if (!isHidden()) {
				t += Math.PI/45;
				final double xa, ya;
				
				switch (direction) {
				case LEFT: xa = -1; ya = 0; break;
				case UP: xa = 0; ya = 1; break;
				case RIGHT: xa = 1; ya = 0; break;
				case DOWN: xa = 0; ya = -1; break;
				default:
					xa = 0; ya = 0;
				}
				
				final double v = Math.abs(amplitude * Math.sin(t));
				final Vector3d position = new Vector3d();
				DialogueArrow.super.getPosition(position);
				DialogueArrow.super.setPosition(x + xa * v,
												y + ya * v,
												position.getZ());
			}
		}
		
	}

}
